#!/bin/bash
#
#
#
################ Colors ################
ESC_SEQ="\x1b["
COL_RESET=$ESC_SEQ"39;49;00m"
RES_COLOR=$ESC_SEQ"39;49;00m"
RED=$ESC_SEQ"31;01m"
GREEN=$ESC_SEQ"32;01m"
YELLOW=$ESC_SEQ"33;01m"
BLUE=$ESC_SEQ"34;01m"
MAGENTA=$ESC_SEQ"35;01m"
CYAN=$ESC_SEQ"36;01m"
########################################

launchpad="$PWD"
basename="${PWD##*/}"

argCounter=1;
CLIsuffix=false
noninteractive=false
cd ..
destination="$PWD""/"
cd -
suffix="duplicate"

for iarg in $*; do
    CLIinputs[$argCounter]=$iarg

    option=`echo "$iarg" | cut -d"=" -f1`

    if [ "$option" == "--suffix" ]; then
	CLIsuffix=true
	suffix=`echo "$iarg" | cut -d"=" -f2`
	echo -e "the suffix will be "$RED$suffix$COL_RESET
    fi
    
    if [ "$option" == "--destination" ]; then
	destination=`echo "$iarg" | cut -d"=" -f2`"/"
	echo -e "the destination will be "$RED$destination$COL_RESET
    fi

    if [ "$option" == "-f" ]; then
	noninteractive=true
	echo -e $RED"this is a non-interactive run"$COL_RESET
    fi

    if [ "$option" == "-h" -o "$option" == "--help" ]; then
 
	echo -e "This script has to be launched from the process folder."
	echo -e "Available options:"
	echo -e "-f"
	echo -e "--destination=path (default is ../)"
	echo -e "--suffix=customsuffix (default is _duplicate)"
	exit
    fi

    let argCounter=argCounter+1
done

echo "The inputs are "${CLIinputs[*]}
thisisamgroot=false

while [ `echo $PWD | wc -c` -gt 2 -a "$thisisamgroot" = false ]; do

    if [ ! -f "./bin/mg5_aMC" ]; then
	echo -e $RED"This has to find where is the MG root folder"$COL_RESET
	echo "This is the directory: "${PWD##*/}
	echo  "... going to parent directory"
	cd ..
	pwd
    else 
	echo "it appears this is a MG5 root directory"
	thisisamgroot=true
    fi
done

basefile="$launchpad"/Cards/proc_card_mg5.dat
echo "basefile="$basefile
###################################basepath=`cat $basefile | grep -e ^output\ | cut -d" " -f2 | rev | cut -c2- | rev`

###################################basepath=`cat $basefile | grep -e ^output\ | cut -d" " -f2 | rev | sed 's@/@@' | rev`
#basepath=`cat "$basefile" | grep -e ^output\ | cut -d" " -f2 | rev | sed 's@^/@@' | rev`
#echo "basepath="$basepath
###################################basepath=$(grep -oP '^output\s+\K.+(?=/)' "$basefile")
###################################basepath=`cat $basefile | grep -e ^output\ | cut -d" " -f2 | sed -n "/^output / {s///; s,\(.*\)/,\1,p}"`
###################################echo "basepath="$basepath
###################################cat R32/uu2uu_check/Cards/proc_card_mg5.dat | grep -e ^output\ | cut -d" " -f2 | rev | sed 's@/@@' | rev

finalpath="$destination""$basename""_""$suffix""/"
echo -e "New process will be generated in the directory "$MAGENTA$finalpath$RES_COLOR


defaultpath=$finalpath
if [ "$noninteractive" = false ]; then
    echo -e "Please check/edit the path for the duplicate"
    ####################################
    echo -e "The current path is is "$MAGENTA$finalpath$COL_RESET
    echo -n "use it? (return=yes, or type new one) " && read finalpath && echo $finalpath
    usethispath=${finalpath:-$defaultpath}
else
usethispath="$defaultpath"
fi

echo $usethispath "will be used"
cat "$basefile" | sed "s@^output\ .*@output\ $usethispath@g" > mytempproccard

diff "$basefile" mytempproccard

if [ "$noninteractive" = false ]; then
    read -p "Are you sure? " -n 1 -r
    echo    # (optional) move to a new line
fi

if [[ $REPLY =~ ^[Yy]$ || "$noninteractive" = true ]]
then
#    echo  "do dangerous stuff"

    ./bin/mg5_aMC mytempproccard
    
    #cp $1/Cards/param_card.dat $finalpath/Cards/param_card.dat
    #cp $1/Cards/run_card.dat $finalpath/Cards/run_card.dat
    #cp $1/Cards/plot_card.dat $finalpath/Cards/plot_card.dat
    
    for x in `ls "$launchpad"/Cards/*.dat`; do 
	
	if [ "$x" != "$launchpad/Cards/proc_card_mg5.dat" ]; then
	    cp -v $x $usethispath"/Cards"
	fi
	
    done

    #    cp mytempproccard $finalpath/Cards/proc_card_mg5.dat

fi
